package com.heatup.apt.api.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.heatup.apt.api.service.HotRecomService;
import com.heatup.apt.common.dao.CourseInfoMapper;
import com.heatup.apt.common.dao.HotRecomMapper;
import com.heatup.apt.common.util.PageBean;
import com.heatup.apt.common.util.StringUtil;
import com.heatup.apt.model.api.ApiResultModel;
import com.heatup.apt.model.api.HotCoachVideoResultModel;
import com.heatup.apt.model.api.HotCoursePicResultModel;
import com.heatup.apt.model.api.HotartResultModel;
import com.heatup.apt.model.api.MostpopCourseResultModel;

import org.springframework.stereotype.Service;

/**
 * 热点推荐业务层
 * <P>File name : HotRecomServiceImpl.java </P>
 * <P>Author : fangxiaowen </P> 
 * <P>Date : 2016年8月22日 </P>
 */
@Service("hotRecomService")
public class HotRecomServiceImpl implements HotRecomService {

	@Resource
	private HotRecomMapper hotRecomMapper;
	@Resource
	private CourseInfoMapper courseInfoMapper;
	
	/**
	 * 热点教练视频
	 * <P>Author : fangxiaowen </P>      
	 * <P>Date : 2016年8月22日 </P>
	 * @param page
	 * @return
	 * @see HotRecomService#getHotCoachList(PageBean)
	 */
	@Override
	public ApiResultModel getHotCoachList(PageBean page) {
		ApiResultModel<Map<Object, Object>> apiResultModel = new ApiResultModel<Map<Object, Object>>();
		Map<Object, Object> map = new HashMap<Object, Object>();
		
		List<HotCoachVideoResultModel> elements = getHotCoachList(page.getPageNo() * page.getPageSize(),page.getPageSize());
		for (HotCoachVideoResultModel hotCoachVideoResultModel : elements) {
			if (StringUtil.isNotEmpty(hotCoachVideoResultModel.getDuration())) {
				hotCoachVideoResultModel.setDuration(hotCoachVideoResultModel.getDuration() + "分钟");
			}
		}
	    map.put("hot_coach_list", elements);
	    map.put("hot_coach_count", hotRecomMapper.getHotCoachCount());
	    
	    apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
	    apiResultModel.setData(map);
		return apiResultModel;
	}
	
	private List<HotCoachVideoResultModel> getHotCoachList(int pageNo,int pageSize){
		Map<Object, Object> params = new HashMap<Object, Object>(); 
		params.put("pageNo", pageNo);
		params.put("pageSize", pageSize);
		List<HotCoachVideoResultModel> elements = hotRecomMapper.getHotCoachList(params);
		return elements;
	}

	/**
	 * 最受欢迎的团体课
	 * <P>Author : fangxiaowen </P>      
	 * <P>Date : 2016年8月23日 </P>
	 * @param page
	 * @return
	 * @see HotRecomService#getMostpopCourseList(PageBean)
	 */
	@Override
	public ApiResultModel getMostpopCourseList(PageBean page) {
		ApiResultModel<Map<Object, Object>> apiResultModel = new ApiResultModel<Map<Object,Object>>();
		apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
	    Map<Object, Object> map = new HashMap<Object, Object>();
	    map.put("mostpop_course_list", getMostpopCourseList(page.getPageNo() * page.getPageSize(),page.getPageSize()));
	    apiResultModel.setData(map);
		return apiResultModel;
	}

	private List<MostpopCourseResultModel> getMostpopCourseList(int pageNo,int pageSize){
		Map<Object, Object> params = new HashMap<Object, Object>(); 
		params.put("pageNo", pageNo);
		params.put("pageSize", pageSize);
		List<MostpopCourseResultModel> elements = hotRecomMapper.getMostpopCourseList(params);
		return elements;
	}
	
	/**
	 * 热门文章
	 * <P>Author : fangxiaowen </P>      
	 * <P>Date : 2016年8月23日 </P>
	 * @param page
	 * @return
	 * @see HotRecomService#getHotartList(PageBean)
	 */
	@Override
	public ApiResultModel getHotartList(PageBean page){
		ApiResultModel<Map<Object, Object>> apiResultModel = new ApiResultModel<Map<Object,Object>>();
		apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
        Map<Object, Object> map = new HashMap<Object, Object>();
        map.put("hotart_list", getHotartList(page.getPageNo() * page.getPageSize(),page.getPageSize()));
        apiResultModel.setData(map);
		return apiResultModel;
	}
	
	private List<HotartResultModel> getHotartList(int pageNo,int pageSize){
		Map<Object, Object> params = new HashMap<Object, Object>(); 
		params.put("pageNo", pageNo);
		params.put("pageSize", pageSize);
		List<HotartResultModel> elements = hotRecomMapper.getHotartList(params);
		return elements;
	}

	
	/**
	 * 热门课程
	 * <P>Author : fangxiaowen </P>      
	 * <P>Date : 2016年8月26日 </P>
	 * @param page
	 * @return
	 * @see HotRecomService#getHotBannerList(PageBean)
	 */
	@Override
	public ApiResultModel getHotBannerList(PageBean page) {
		ApiResultModel<Map<Object, Object>> apiResultModel = new ApiResultModel<Map<Object,Object>>();
		apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
        Map<Object, Object> map = new HashMap<Object, Object>();
        map.put("hot_banner_list", getHotBannerList(page.getPageNo() * page.getPageSize(),page.getPageSize()));
        apiResultModel.setData(map);
		return apiResultModel;
	}
	
	private List<HotCoursePicResultModel> getHotBannerList(int pageNo,int pageSize){
		Map<Object, Object> params = new HashMap<Object, Object>(); 
		params.put("pageNo", pageNo);
		params.put("pageSize", pageSize);
		List<HotCoursePicResultModel> elements = hotRecomMapper.getHotBannerList(params);
		return elements;
	}

	@Override
	public ApiResultModel getHotMainInfo() {
		ApiResultModel<Map<Object, Object>> apiResultModel = new ApiResultModel<Map<Object,Object>>();
        Map<Object, Object> map = new HashMap<Object, Object>();
        //横幅
        map.put("hot_banner_list", getHotBannerList(0, 5));
        //热门教练视频
        map.put("hot_coach_list", getHotCoachList(0,2));
        //最受欢迎团体课
        map.put("mostpop_course_list", getMostpopCourseList(0,2));
        //热门文章
        map.put("hotart_list", getHotartList(0,3));
        apiResultModel.setData(map);
        apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
		return apiResultModel;
	}
	
}
